(function() {
  console.log('index.js');
  var app = new Vue({
    el: '#app',
    data: {
      tbUser: {
        username: '',
        password: ''
      },
      user: {},
      error: '',
      list: []
    },
    methods: {
      query: query,
      login: login,
      getUser: getUser,
      logout: logout
    }
  });

  function query() {
    huhuiyu.send('/user/queryAll', {}, function(data) {
      if (data.success) {
        app.list = data.datas.list;
        return;
      }
      alert(data.message);
    });
  }

  function login() {
    app.error = '';
    //开始登录
    if (this.inaction) {
      app.error = '登录中..';
      return;
    }
    //切换成正在登录
    if (!app.tbUser.username) {
      app.error = '用户名必须填写';

      return;
    }
    if (!app.tbUser.password) {
      app.error = '密码必须填写';

      return;
    }
    huhuiyu.send(
      '/user/login',
      {
        tbUser: app.tbUser
      },
      function(data) {
        console.log(data);
        app.tbUser.password = '';
        if (data.success) {
          alert('登录成功！');
          location = 'index.html';
          return;
        }
        app.error = data.message;
      }
    );
  }

  function getUser() {
    huhuiyu.send('/user/getUserInfo', {}, function(data) {
      console.log(data);
      if (data.datas.user) {
        app.user = data.datas.user;
        return;
      }
    });
  }

  //安全退出
  function logout() {
    huhuiyu.send('/user/logout', {}, function(data) {
      console.log(data);
      alert(data.message);
      app.user = null;
    });
  }

  getUser();
  query();
})();
